# -*-coding:utf-8-*-

from pymongo import MongoClient
import datetime
import time

todayStr = datetime.datetime.now().strftime('%Y_%m_%d')
date_database = (datetime.datetime.strptime(todayStr, '%Y_%m_%d') - datetime.timedelta(days=1)).strftime(
    '%Y_%m_%d')  # 选择前一天爬取的数据库

uri = "mongodb://XK:xkkj2014@39.108.174.188:27017/admin"
client = MongoClient(uri)
db = client['ClinicalTrials']
# data = db['backup']
log = db['trial_log']
data = db['data']
db2 = client['final_data']
data_push = db['update_data']  # 这个就是最终的数据库
new_data = db2[date_database]


def addRecords():
    added_records = set([r.get('Identifier') for r in new_data.find()]).difference(
        [r.get('Identifier') for r in data.find()])
    for added_record in added_records:
        rec_add = new_data.find_one({'Identifier': added_record})
        rec_add['created_at'] = datetime.datetime.now().strftime('%Y-%m-%d')  # 有created_at 的就是添加的。
        data.save(rec_add)
        data_push.save(rec_add)

    return len(added_records)


def updateRecords():
    updates = 0
    for old_record in data.find({"Source": {"$in": ["药监局", "ChiCTR", "Clinical Trial"]}}):
        updated = False
        new_record = new_data.find_one({'Identifier': old_record.get('Identifier')})
        if new_record:
            for key, value in new_record.items():
                if key != '_id' and old_record.get(key) != value:
                    old_record[key] = value
                    updated = True
            if updated:
                data.save(old_record)
                updates += 1

    return updates


def delOldFinalData():
    """删除 final_data 旧的备份数据"""
    def calTime(date1, date2):
        date1 = time.strptime(date1, "%Y_%m_%d")
        date2 = time.strptime(date2, "%Y_%m_%d")
        date1 = datetime.datetime(date1[0], date1[1], date1[2])
        date2 = datetime.datetime(date2[0], date2[1], date2[2])
        return date1 - date2
    cols = db2.collection_names()
    for c in cols:
        ct = calTime(todayStr, c)
        if ct.days >= 7:
            db2[c].drop()
            print('drop final_data', c)

def getFinalData(skip, limit=100):
    ret = []
    for t in new_data.find({'Source':'药监局'}).limit(limit).skip(skip*limit):
        ret.append(t)
    return ret

def getAllNewData():
    skip = 0
    result = []
    while True:
        ret = getFinalData(skip)
        if ret:
            result += ret
            skip += 1
        else:
            return result

def compareTrial(t1, t2):
    diff = []
    for k, v in t1.items():
        try:
            t2 = t2[k]
        except:
            t2 = ''
        if v != t2[k]:
            diff.append(k)
    return diff

def updateCtrTrials():
    new_trials = getAllNewData()
    for t in new_trials:
        id = t['Identifier']
        old_trial = data.find_one({'Identifier':id})
        if old_trial:
            curState = t['State']
            oldState = old_trial['State']
            if curState!= oldState:
                log.insert({
                    'date': todayStr,
                    'Identifier':id,
                    'State': curState
                })
            data.update({'Identifier':id},{'$set':{'State':curState, 'updated': todayStr}})
        else:
            t['created_time'] = todayStr
            data.insert(t)






if __name__ == '__main__':
    #adds = addRecords()
    #ups = updateRecords()
    #print("Successfully updated data, added {0} records, updated {1} records".format(adds, ups))
    #delOldFinalData()
    index = 0
    for t in new_data.find({'Source': '药监局'}).limit(10).skip(1000):
        print(index, t)
        index += 1